package com.dc.datacollect.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dc.datacollect.conduit.R;
import com.dc.datacollect.constant.BaseConstant;
import com.dc.datacollect.constant.PermitNameConstant;
import com.dc.datacollect.constant.type.UserType;
import com.dc.datacollect.dto.SysUserDTO;
import com.dc.datacollect.dto.input.AlterPasswordInput;
import com.dc.datacollect.dto.input.LoginInput;
import com.dc.datacollect.dto.input.RegisterInput;
import com.dc.datacollect.dto.input.UpdateUserInfoInput;
import com.dc.datacollect.dto.output.SysUserInfoOutput;
import com.dc.datacollect.entity.Collector;
import com.dc.datacollect.entity.User;
import com.dc.datacollect.helpers.RememberMeHelper;
import com.dc.datacollect.helpers.VerificationCodeProvide;
import com.dc.datacollect.service.CollectorService;
import com.dc.datacollect.service.IndexService;
import com.dc.datacollect.service.PermitService;
import com.dc.datacollect.service.UserService;
import com.dc.datacollect.utils.MD5Util;
import com.dc.datacollect.utils.ReflectionUtils;
import com.dc.datacollect.helpers.SessionProvide;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.dc.datacollect.entity.Permit;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    private UserService userService;
    @Autowired
    private CollectorService collectorService;
    @Autowired
    private PermitService permitService;

    @Autowired
    private IndexService indexService;

    @Autowired
    private VerificationCodeProvide verificationCodeProvide;

    @RequestMapping({"/","/login.html"})
    public String index(Model model) {
        try {
            if (indexService.tryRememberMeLogin()) {
                // 去管理页面
                return "redirect:/manage.html";
            }
        } catch (Exception e) {}
        return "login";
    }
    @RequestMapping({"/register.html"})
    public String register() {
        return "register";
    }


    @RequestMapping({"/manage.html"})
    public String manage(Model model) {
        SysUserDTO loginObject = SessionProvide.getLoginObject();
        model.addAttribute("type",loginObject.getType());
        model.addAttribute("username",loginObject.getUsername());
        model.addAttribute("nickname",loginObject.getNickname());
        List<Permit> permitTree = permitService.getPermitTree(loginObject.getType());
        model.addAttribute("permitTree",permitTree);
        model.addAttribute("IN_PROGRESS", PermitNameConstant.IN_PROGRESS);
        return "manage/index";
    }
    @RequestMapping({"/logout"})
    public String logout(Model model) {
        // 取消记住我
        RememberMeHelper.cancelRememberMe();
        // 删除当前用户的相关会话信息
        SessionProvide.remove(BaseConstant.USER_SESSION_KEY);
        return "login";
    }
    @PostMapping("/login")
    @ResponseBody
    public R login(@Valid @RequestBody LoginInput loginInput) throws Exception {
        return R.auto("登录", indexService.login(loginInput));
    }

    @PostMapping("/register")
    @ResponseBody
    public R register(@Valid @RequestBody RegisterInput registerInput) throws Exception {
        return R.auto("注册-登录",indexService.register(registerInput));
    }
    @PostMapping("/alterPassword")
    @ResponseBody
    public R alterPassword(@Valid @RequestBody AlterPasswordInput alterPasswordInput) throws Exception {
        Boolean isSuccess = indexService.alterPassword(alterPasswordInput);
        return R.auto("修改密码",isSuccess);
    }
    @GetMapping("/sendCode")
    @ResponseBody
    public R sendCode(@Pattern(regexp = "^.*@.*\\..*$",message = "邮箱格式不正确") @RequestParam("email") String email) {
        return R.auto("发送验证码",indexService.sendCode(email));
    }
    @PostMapping("/updateUserInfo")
    @ResponseBody
    public R updateUserInfo(@ModelAttribute UpdateUserInfoInput updateUserInfoInput) throws Exception {
        return R.auto("更新用户信息",indexService.updateUserInfo(updateUserInfoInput));
    }

    @GetMapping("/getUserInfo")
    @ResponseBody
    public R getUserInfo() throws Exception {
        SysUserInfoOutput sysUserInfoOutput = indexService.getUserInfo();
        return R.ok().data(sysUserInfoOutput);
    }

}
